<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="条码类型名称" prop="barcodeTypeName">
        <el-input v-model="queryParams.barcodeTypeName" placeholder="请输入产品规格" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd(null)" v-hasPermi="['produce:barcodeTemplet:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-edit" size="mini" @click="handleDel" v-hasPermi="['produce:barcodeTemplet:remove']">批量删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="workorderSnList" @selection-change="handleSelectionChange" height="calc(100vh - 310px)">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="条码类型名称" align="center" prop="barcodeTypeName" />

      <el-table-column label="长度" align="center" prop="weight" />

      <el-table-column label="宽度" align="center" prop="heigh" />

      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="workorderTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
          <!-- <span>{{ parseTime(scope.row.workorderTime, "{y}-{m}-{d}") }}</span> -->
        </template>
      </el-table-column>
      <!--      <el-table-column label="工单批次" align="center" prop="workorderBatch" />-->
      <!--      <el-table-column label="SN状态" align="center" prop="status" />-->
      <el-table-column label="修改人" align="center" prop="updateBy" />
      <el-table-column label="修改时间" align="center" prop="workorderTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
          <!-- <span>{{ parseTime(scope.row.workorderTime, "{y}-{m}-{d}") }}</span> -->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleReplace(scope.row)" v-hasPermi="['produce:barcodeTemplet:view']">查看模板</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAdd(scope.row)" v-hasPermi="['produce:barcodeTemplet:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" :page-sizes="[50, 100, 200, 300, 400, 500]" @pagination="getList" />
    <!-- 产品调试报告 -->
    <!-- 添加或修改模板 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模板名称" prop="barcodeTypeName">
          <el-input v-model="form.barcodeTypeName" placeholder="模板名称" />
        </el-form-item>
        <el-form-item label="长度" prop="weight">
          <el-input v-model="form.weight" placeholder="高度" />
        </el-form-item>
        <el-form-item label="宽度" prop="heigh">
          <el-input v-model="form.heigh" placeholder="宽度" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  -->
    <ReplaceSn v-if="replaceVisible" ref="ReplaceSn"></ReplaceSn>
  </div>
</template>

<script>
import {
  delBarcodeM,
  updateBarcodeM,
  addBarcodeM,
  getBarcodeMList,
} from "@/api/mes/pro/barcodeManagement";
import { } from "@/api/mes/pro/barcodeManagement";
import ReplaceSn from "./replaceSn.vue";
import QRCode from "qrcodejs2";
import JsBarcode from "jsbarcode";
export default {
  name: "BarcodeManagement",
  components: { ReplaceSn },
  dicts: ["fqc_is_check"],
  data() {
    return {
      isAdd: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //sncode集合
      sncodeList: [],
      //转成字符串的sncode
      snCodes: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 产品调试报告
      drawerVisible: false,
      //更换SN码
      replaceVisible: false,
      //批量更换sn码
      batchVisible: false,
      //打印SN码
      printVisible: false,
      reportSn: null,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单SN码表格数据
      workorderSnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 详情弹窗
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        barcodeTypeName: null,
      },
      // 表单参数
      form: {
        barcodeTypeName: null,
        weight: null,
        heigh: null,
      },
      // 表单校验
      rules: {},
      materialList: [],
      formDetail: {},
      proYards: {},
      batchList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工单SN码列表 */
    getList() {
      this.loading = true;
      getBarcodeMList(this.queryParams).then((response) => {
        this.workorderSnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelDetail() {
      this.openDetail = false;
      this.resetDetail();
    },

    // 表单重置
    reset() {
      this.form = {
        barcodeTypeName: null,
        weight: null,
        heigh: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      //id集合
      this.ids = selection.map((item) => item.id);
      //sncode集合
      this.sncodeList = selection.map((item) => item.snCode);
      //转成字符串
      this.snCodes = this.sncodeList.join(", ");
      console.log(this.ids, "多选框的数据id");
      console.log(this.snCodes, "多选框的数据code");
      this.queryParams.proWorkorderSnIdList = selection.map(
        (item) => item.snId
      );
      this.batchList = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      if (row) {
        //row 有值  修改
        console.log(row);
        this.reset();
        this.form = JSON.parse(JSON.stringify(row));
        this.isAdd = false;
        console.log("11111111");

        this.open = true;
        this.title = "修改模板";
      } else {
        //row 为null 新增
        this.isAdd = true;
        console.log("11111111");
        this.reset();
        this.open = true;
        this.title = "添加模板";
      }
    },

    /** 批量修改按钮操作 */
    handleDel() {
      if (this.ids.length === 0) {
        this.$modal.msgWarning("请选择要删除的数据项");
        return;
      }
      this.$modal.confirm("是否确认删除？").then(() => {
        // console.log(this.ids, "批量删除");
        // console.log(this.snCodes, "批量删除");
        delBarcodeM(this.ids).then((response) => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBarcodeM(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBarcodeM(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const snIds = row.snId || this.ids;
      this.$modal
        .confirm('是否确认删除工单SN码编号为"' + snIds + '"的数据项？')
        .then(function () {
          return delWorkorderSn(snIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/pro/workorderSn/export",
        {
          ...this.queryParams,
        },
        `workorderSn_${new Date().getTime()}.xlsx`
      );
    },
    // 产看产品调试报告
    handleReport(row) {
      this.reportSn = row.snCode;

      this.drawerVisible = true;
    },
    //更换SN码
    handleReplace(row) {
      this.reportSn = row.snCode;
      this.replaceVisible = true;
      this.$nextTick(() => {
        this.$refs.ReplaceSn.init(row);
      });
    },
    //打印车载SN码
    vehiclePrint(row, type) {
      let obj = [];
      if (type == "0") {
        // 批量打印
        obj = this.batchList;
        for (let i = 0; i < obj.length; i++) {
          const element = obj[i].workshopId;
          if (element !== 208) {
            this.$message.warning("请选择车载产品打印！！！");
            return false;
          }
        }
        this.batchList;
      } else {
        obj.push(row);
      }
      console.log(obj, "=====obj===");
      this.printVisible = true;
      this.$nextTick(() => {
        this.$refs.print.init(obj);
      });
    },
    print(row) {
      console.log(row);
      let param = [];
      param.push(row);
      console.log(param, "=====打印参数==param======");
      printSn(param).then((response) => {
        this.$modal.msgSuccess(response);
        // if (response.code == 200) {
        //   this.$modal.msgSuccess("打印成功");
        // } else {
        //   this.$modal.msgError("打印失败");
        // }
      });
    },
    // 批量打印sn码
    batchPrint() {
      if (this.batchList && this.batchList.length > 0) {
        printSn(this.batchList).then((response) => {
          console.log(response, "============response============");
          var dt = response + "";
          if (dt.indexOf("打印成功") != -1) {
            this.$modal.msgSuccess("打印成功");
          } else {
            this.$modal.msgError("打印失败");
          }
        });
      } else {
        this.$modal.msgError("请选择数据");
      }
    },
    resetDetail() {
      this.formDetail = {};
      this.materialList = [];
      this.proYards = {};
      this.$refs.qrCodeUrl.innerHTML = "";
      this.$refs.qrCodeUrl1.innerHTML = "";
      this.resetForm("formDetail");
    },
    handleView(row) {
      this.reset();
      const snCode = row.snCode || null;
      detailsWorkorderSn(snCode).then((response) => {
        this.formDetail = response.data?.proWorkorderSn || {};
        this.materialList = response.data?.materialList || [];
        this.proYards = response.data?.proYards || {};
        this.openDetail = true;
      });
    },
    snOpen() {
      console.log(
        document.getElementById("jsbarcode1"),
        this.$refs.qrCodeUrl,
        "====document.getElementById)====="
      );
      this.creatQrCode();
      JsBarcode(document.getElementById("jsbarcode1"), this.formDetail.snCode);
    },
    creatQrCode() {
      var qrcode = new QRCode(this.$refs.qrCodeUrl, {
        text: this.proYards.assemblyCode, // 需要转换为二维码的内容
        width: 100,
        height: 100,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H,
      });
      var qrcode1 = new QRCode(this.$refs.qrCodeUrl1, {
        text: this.proYards.aftersaleCode, // 需要转换为二维码的内容
        width: 100,
        height: 100,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H,
      });
    },
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.app-container ::v-deep .el-drawer__header > :first-child {
  font-weight: 700 !important;
  font-size: 20px;
}
.yardsContent {
  margin-top: 20px;
  // text-align: center;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  div {
    font-size: 18px;
  }
}
.yardsDiv {
  margin: 0 10px;
  display: flex;
  flex-direction: column;
  // justify-content: center;
  align-items: center;
  margin-bottom: 15px;
  button {
    margin-bottom: 10px;
    font-size: 18px;
  }
  .codetext {
    margin-top: 10px;
  }
}
.drawerFooter {
  text-align: right;
  padding: 20px;
}
.bg-purple {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  // span:nth-child(1){
  //   display: inline-block;
  //   width: 110px;
  //   white-space: nowrap;
  //   line-height: 30px;
  // }
}
</style>
